//
//  ExamineController.m
//  DMGME
//
//  Created by ihope99 on 15/9/8.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "ExamineController.h"
#import "TitleTypeView.h"
@interface ExamineController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ExamineController
{
    TitleTypeView *_titleTypeView;
    UITableView *tableview;
    int _type;
    NSArray *arrStrLeft;
    NSArray *arrStrCenter;
    NSArray *arrStrRighe;
    
    NSArray *arrStrLeftId;
    NSArray *arrStrCenterId;
    NSArray *arrStrRigheId;
    NSMutableArray *arrProvInfo;
    NSMutableArray *arrcityInfo;
    NSMutableArray *arrcountryInfo;
    NSMutableArray *arrProvInfoId;
    NSMutableArray *arrcityInfoId;
    NSMutableArray *arrcountryInfoId;
}
- (void)shopIdStr:(void (^)(NSString *, NSString *))block
{
    self.ShopIdStr = block;
}
- (void)threeeTypeNet{
    
    arrStrLeft =@[@""];
    arrStrCenter =@[@""];
    arrStrRighe =@[@""];
    
//    arrStrRigheId = [[NSArray alloc] init];

    
    NSArray *aray=@[@"区/县代理",@"市代理",@"省代理"];
    _titleTypeView=[[TitleTypeView alloc]initWithFrame:CGRectMake(0, 60,self.view.frame.size.width,40) WithBtuName:aray WithScrollView:nil] ;
    _titleTypeView.backgroundColor = [UIColor whiteColor];
    [_titleTypeView btuSelectWith:^(int tag) {
//        NSLog(@"kkk%d",tag);
        _type=tag;
        [tableview reloadData];
    }];
    [self.view addSubview:_titleTypeView];
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0, 101, KWidth, 1);
    lineView.backgroundColor = ComColor;
    [self.view addSubview:lineView];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$parentAgent"] forKey:@"token"];
    [dict setValue: @"parentAgent" forKey:@"action"];
    [dict setValue: _mDicID[@"省"] forKey:@"province_id"];
    [dict setValue: _mDicID[@"市"] forKey:@"city_id"];
    [dict setValue: _mDicID[@"县/区"] forKey:@"country_id"];
    
    
    [arrProvInfo removeAllObjects];
    [arrProvInfoId removeAllObjects];
    [arrcityInfo removeAllObjects];
    [arrcityInfoId removeAllObjects];
    [arrcountryInfo removeAllObjects];
    [arrcountryInfoId removeAllObjects];
    
    
    arrProvInfo = [NSMutableArray array];
    arrcityInfo = [NSMutableArray array];
    arrcountryInfo = [NSMutableArray array];
    
    arrProvInfoId = [NSMutableArray array];
    arrcityInfoId = [NSMutableArray array];
    arrcountryInfoId = [NSMutableArray array];
    
    [AFDownManager POST:REGISTERNET parameters:dict success:^(id responseObject) {
//        NSLog(@"%@",responseObject);
        if ([responseObject[@"data"][@"province"] count] == 0) {
//            return ;
        }else{
            for (int i = 0; i<=[responseObject[@"data"][@"province"] count]-1; i++) {
                NSString *nameAndPhone;
                NSString *manageId;
                nameAndPhone = [NSString stringWithFormat:@"%@ %@",responseObject[@"data"][@"province"][i][@"realname"],responseObject[@"data"][@"province"][i][@"mobile"]];
                manageId = responseObject[@"data"][@"province"][i][@"id"];
                [arrProvInfo addObject:nameAndPhone];
                [arrProvInfoId addObject:manageId];
                arrStrRighe = arrProvInfo;
                arrStrRigheId = arrProvInfoId;
            }
        }
        
        if ([responseObject[@"data"][@"city"] count] == 0) {
//            return ;
            
        }else{
            for (int i = 0; i<=[responseObject[@"data"][@"city"] count]-1; i++) {
                NSString *nameAndPhone ;
                NSString *manageId;

                nameAndPhone = [NSString stringWithFormat:@"%@ %@",responseObject[@"data"][@"city"][i][@"realname"],responseObject[@"data"][@"city"][i][@"mobile"]];
                manageId = responseObject[@"data"][@"city"][i][@"id"];
                [arrcityInfo addObject:nameAndPhone];
                [arrcityInfoId addObject:manageId];
                arrStrCenter = arrcityInfo;
                arrStrCenterId = arrcityInfoId;
            }}
        
        if ([responseObject[@"data"][@"country"] count] == 0) {
//            return ;
        }else{
            
            for (int i = 0; i<=[responseObject[@"data"][@"country"] count]-1; i++) {
                NSString *nameAndPhone ;
                NSString *manageId;

                nameAndPhone = [NSString stringWithFormat:@"%@ %@",responseObject[@"data"][@"country"][i][@"realname"],responseObject[@"data"][@"country"][i][@"mobile"]];
                manageId = responseObject[@"data"][@"country"][i][@"id"];
                [arrcountryInfo addObject:nameAndPhone];
                [arrcountryInfoId addObject:manageId];
                arrStrLeft = arrcountryInfo;
                arrStrLeftId = arrcountryInfoId;
            }
        }
//        NSLog(@"%@",arrStrLeft);
     
        [tableview reloadData];
        
    } failure:^(NSError *error) {
    }];
}
- (void)twoTypNet{
    
    [arrProvInfo removeAllObjects];
    [arrProvInfoId removeAllObjects];
    [arrcityInfo removeAllObjects];
    [arrcityInfoId removeAllObjects];
    
    arrStrLeft =@[@""];
    arrStrCenter =@[@""];
    
    NSArray *aray=@[@"市代理",@"省代理"];
    _titleTypeView=[[TitleTypeView alloc]initWithFrame:CGRectMake(0, 60,self.view.frame.size.width,40) WithBtuName:aray WithScrollView:nil] ;
    _titleTypeView.backgroundColor = [UIColor whiteColor];
    [_titleTypeView btuSelectWith:^(int tag) {
//        NSLog(@"kkk%d",tag);
        _type=tag;
        [tableview reloadData];
    }];
    [self.view addSubview:_titleTypeView];
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0, 101, KWidth, 1);
    lineView.backgroundColor = ComColor;
    [self.view addSubview:lineView];
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$parentAgent"] forKey:@"token"];
    [dict setValue: @"parentAgent" forKey:@"action"];
    [dict setValue: _mDicID[@"省"] forKey:@"province_id"];
    [dict setValue: _mDicID[@"市"] forKey:@"city_id"];
    
    arrProvInfo = [NSMutableArray array];
    arrcityInfo = [NSMutableArray array];
    arrProvInfoId = [NSMutableArray array];
    arrcityInfoId = [NSMutableArray array];
    
    [AFDownManager POST:REGISTERNET parameters:dict success:^(id responseObject) {
//        NSLog(@"responseObject == %@",responseObject);
        if ([responseObject[@"data"][@"province"] count] == 0) {
            return ;
        }else{
        
        for (int i = 0; i<=[responseObject[@"data"][@"province"] count]-1; i++) {
            NSString *nameAndPhone ;
            NSString *manageId ;
            nameAndPhone = [NSString stringWithFormat:@"%@ %@",responseObject[@"data"][@"province"][i][@"realname"],responseObject[@"data"][@"province"][i][@"mobile"]];
            manageId = responseObject[@"data"][@"province"][i][@"id"];
            [arrProvInfo addObject:nameAndPhone];
            [arrProvInfoId addObject:manageId];
            arrStrCenter = arrProvInfo;
            arrStrCenterId = arrProvInfoId;
        }
            }
        
        if ([responseObject[@"data"][@"city"] count] == 0) {
            return ;
        }else{
           
//        for (int i = 0; i<[responseObject[@"data"][@"city"] count]; i++) {
//            NSString *nameAndPhone ;
//            NSString *manageId ;
//
//            nameAndPhone = [NSString stringWithFormat:@"%@ %@",responseObject[@"data"][@"city"][i][@"realname"],responseObject[@"data"][@"city"][i][@"mobile"]];
//            manageId = responseObject[@"data"][@"province"][i][@"id"];
//
//            [arrcityInfo addObject:nameAndPhone];
//            [arrcityInfoId addObject:manageId];
//
//            arrStrLeft = arrcityInfo;
//            arrStrLeftId = arrcityInfoId;
//          
//
//        }
            NSArray *arr = responseObject[@"data"][@"city"];
            for (NSDictionary *dic in arr) {
                NSString *nameAndPhone = [NSString stringWithFormat:@"%@%@",dic[@"realname"],dic[@"mobile"]];
                NSString *manageId = [NSString stringWithFormat:@"%@",dict[@"id"]];
                [arrcityInfo addObject:nameAndPhone];
                [arrcityInfoId addObject:manageId];
                
                arrStrLeft = arrcityInfo;
                arrStrLeftId = arrcityInfoId;
                
            }
            

        }
                [tableview reloadData];
        
    } failure:^(NSError *error) {
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
   
    
    if ([_typeStr isEqualToString:@"2"]) {
        [self twoTypNet];
    }else{
        [self threeeTypeNet];
    }
    self.title = @"选择审核代理商";
    self.view.backgroundColor = [UIColor whiteColor];
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    
    UILabel *titileLable = [[UILabel alloc] init];
    titileLable.frame = CGRectMake(15, 15, 200, 18);
    titileLable.font = [UIFont systemFontOfSize:17];
    titileLable.font = [UIFont boldSystemFontOfSize:17];
    titileLable.text = @"选择审核您的代理商级别";
    titileLable.textColor = ComColor;
    [self.view addSubview:titileLable];
    
    
    
    tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,60+42,self.view.frame.size.width, self.view.frame.size.height-42) style:UITableViewStylePlain];
    tableview.delegate=self;
    tableview.dataSource=self;
    tableview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:tableview];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (_type == 0) {
        return arrStrLeft.count;
    }else if (_type == 1){
        return arrStrCenter.count;
    }else{
        return arrStrRighe.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID =@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    if (_type==0) {
        
        cell.textLabel.text =[NSString stringWithFormat:@"%@",arrStrLeft[indexPath.row]];
    }else if(_type == 1)
    {
        cell.textLabel.text =[NSString stringWithFormat:@"%@",arrStrCenter[indexPath.row]];
    }else{
        cell.textLabel.text =[NSString stringWithFormat:@"%@",arrStrRighe[indexPath.row]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    if (_type==0) {
        self.ShopIdStr(arrStrLeft[indexPath.row],arrStrLeftId[indexPath.row]);

    }else if(_type == 1)
    {
        self.ShopIdStr(arrStrCenter[indexPath.row],arrStrCenterId[indexPath.row]);

    }else{
        self.ShopIdStr(arrStrRighe[indexPath.row],arrStrRigheId[indexPath.row]);

    }
    [self.navigationController popViewControllerAnimated:YES];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
